package com.lh.java.basic.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestThreadPool {
	public static void main(String args[]) throws InterruptedException {
		// only two threads
		ExecutorService exec = Executors.newFixedThreadPool(2);
		for (int index = 0; index < 100; index++) {
			Runnable run = new Runnable() {
				public void run() {
					long time = (long) (Math.random() * 20 * 1000);
					System.out.println("run start");
					try {
						Thread.sleep(time);
						System.out.println("i am is " + "Sleeping" + time / 1000
								+ "s");
					} catch (InterruptedException e) {
					}
				}
			};
			exec.execute(run);
			
		}
		// must shutdown
		exec.shutdown();
		Runnable run1 = new Runnable() {
			public void run() {
				long time = (long) (Math.random() * 50 * 1000);	
				System.out.println("run1 start==");
				try {
					Thread.sleep(time);
					System.out.println("---------run1 " + "Sleeping" + time / 1000
							+ "s");
				} catch (InterruptedException e) {
				}
			}
		};
		run1.run();
	}
}